package  lz.controls
{
	import com.common.utils.Debug;
	import com.global.GlobalDef;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import lz.controls.core.UIComponent;

	/**
	 * 基础窗口 可拖动
	 * @author lxh
	 */	
	public class BaseWindow extends UIComponent
	{
		/**
		 *窗口背景 可设拖动 
		 */		
		public var windowBg 			:MovieClip;
		/**
		 *背景宽高 
		 */		
		public var bgWidth 				:Number;
		public var bgHeight 			:Number;
		/**
		 *是否可以拖动 
		 */		
		private var _dragEnabled 		:Boolean;
		/**
		 *是否自适应 
		 */		
		private var _resizeEnabled 	:Boolean;
		
		public function BaseWindow()
		{
			
		}
		/**
		 * 添加事件
		 * @param e
		 * 
		 */		
		protected function addEvent(e:Event = null):void
		{
			windowBg.addEventListener(MouseEvent.MOUSE_DOWN,onDragMouseDown);
			windowBg.addEventListener(MouseEvent.MOUSE_MOVE,onDragMouseMove);
			if(GlobalDef.GameStage)
			{
				GlobalDef.GameStage.addEventListener(MouseEvent.MOUSE_UP,onDragMouseUp);
			}
			else
			{
				windowBg.addEventListener(Event.ADDED_TO_STAGE,onAddToStage);
			}
			windowBg.addEventListener(MouseEvent.MOUSE_OUT,onDragMouseOut);
			windowBg.addEventListener(Event.REMOVED_FROM_STAGE,removeEvent);
		}
		
		private function onAddToStage(e :Event):void
		{
			windowBg.removeEventListener(Event.ADDED_TO_STAGE,onAddToStage);
			GlobalDef.GameStage.addEventListener(MouseEvent.MOUSE_UP,onDragMouseUp);
		}
		/**
		 * 删除事件
		 * @param e
		 * 
		 */		
		protected function removeEvent(e:Event = null):void
		{
			windowBg.removeEventListener(MouseEvent.MOUSE_DOWN,onDragMouseDown);
			windowBg.removeEventListener(MouseEvent.MOUSE_MOVE,onDragMouseMove);
			windowBg.removeEventListener(MouseEvent.MOUSE_OUT,onDragMouseOut);
			windowBg.removeEventListener(Event.REMOVED_FROM_STAGE,removeEvent);
			if(GlobalDef.GameStage)
			{
				GlobalDef.GameStage.removeEventListener(MouseEvent.MOUSE_UP,onDragMouseUp);
				GlobalDef.GameStage.removeEventListener(Event.RESIZE,onResizeHandler);
			}
		}
		/**
		 *拖动 
		 * @param e
		 * 
		 */		
		protected function onDragMouseDown(e:MouseEvent):void
		{
			if(this && this.parent)
			{
				this.parent.setChildIndex(this,this.parent.numChildren - 1);
				var rect :Rectangle = new Rectangle(0,0,GlobalDef.GameStage.stageWidth - windowBg.width,GlobalDef.GameStage.stageHeight - windowBg.height);
				this.startDrag(false,rect);
			}
		}
		/**
		 *移动 
		 * @param e
		 * 
		 */		
		protected function onDragMouseMove(e:MouseEvent):void
		{
			
		}
		/**
		 *移出 
		 * @param e
		 * 
		 */		
		protected function onDragMouseOut(e:MouseEvent):void
		{
			
		}
		/**
		 *松开 
		 * @param e
		 * 
		 */		
		protected function onDragMouseUp(e:MouseEvent):void
		{
			this.stopDrag();
		}
		/**
		 *自适应 
		 * @param e
		 * 
		 */		
		protected function onResizeHandler(e :Event):void
		{
			
		}
		/**
		 *设置是否可拖动 
		 * @param value
		 * 
		 */		
		public function set dragEnabled(value :Boolean):void
		{
			_dragEnabled = value;
			if(windowBg == null)
			{
				return;
			}
			if(_dragEnabled)
			{
				windowBg.mouseChildren = true;
				windowBg.mouseEnabled = true;
				//
				addEvent();
			}
			else
			{
				windowBg.mouseChildren = false;
				windowBg.mouseEnabled = false;
				//
				removeEvent();
			}
		}
		public function get dragEnabled():Boolean
		{
			return _dragEnabled;
		}
		/**
		 *设置是否自适应
		 * @param value
		 * 
		 */		
		public function set resizeEnabled(value :Boolean):void
		{
			_resizeEnabled = value;
			if(_resizeEnabled)
			{
				GlobalDef.GameStage.addEventListener(Event.RESIZE,onResizeHandler);
			}
			else
			{
				GlobalDef.GameStage.removeEventListener(Event.RESIZE,onResizeHandler);
			}
		}
		public function get resizeEnabled():Boolean
		{
			return _resizeEnabled;
		}
	}
}